/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.anny.shop.web;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.anny.shop.entity.AnnyShop;
import com.jeeplus.modules.anny.shop.service.AnnyShopService;

/**
 * 店铺Controller
 * @author linzx
 * @version 2018-10-19
 */
@Controller
@RequestMapping(value = "${adminPath}/anny/shop/annyShop")
public class AnnyShopController extends BaseController {

	@Autowired
	private AnnyShopService annyShopService;
	
	@ModelAttribute
	public AnnyShop get(@RequestParam(required=false) String id) {
		AnnyShop entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = annyShopService.get(id);
		}
		if (entity == null){
			entity = new AnnyShop();
		}
		return entity;
	}

	/**
	 * 获取商家数
	 * @return
	 */
	public  int  businessNum(){
		List<AnnyShop>  annyShop =	annyShopService.businessNum();

		int businessNumber=annyShop.size();
		return businessNumber;
	}
	/**
	 * 店铺列表页面
	 */
	@RequiresPermissions("anny:shop:annyShop:list")
	@RequestMapping(value = {"list", ""})
	public String list(AnnyShop annyShop, Model model) {
		model.addAttribute("annyShop", annyShop);
		return "modules/anny/shop/annyShopList";
	}
	
		/**
	 * 店铺列表数据
	 */
	@ResponseBody
	@RequiresPermissions("anny:shop:annyShop:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(AnnyShop annyShop, HttpServletRequest request, HttpServletResponse response, Model model) {
		try {
			request.setCharacterEncoding("utf-8");
			annyShop.setShopName(new String(request.getParameter("shopName").getBytes("iso8859-1"),"utf-8"));
			annyShop.setBusinessScope(new String(request.getParameter("businessScope").getBytes("iso8859-1"),"utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Page<AnnyShop> page = annyShopService.findPage(new Page<AnnyShop>(request, response), annyShop); 
		return getBootstrapData(page);
	}
	/**
	 * 模糊查询店名
	 * @param shopName
	 * @return
	 */
	public List<AnnyShop> likeShop(String shopName){
		  List<AnnyShop>  shop= annyShopService.likeShop(shopName);
		  return shop;
	}


	/**
	 * 查看，增加，编辑店铺表单页面
	 */
	@RequiresPermissions(value={"anny:shop:annyShop:view","anny:shop:annyShop:add","anny:shop:annyShop:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(AnnyShop annyShop, Model model) {
		model.addAttribute("annyShop", annyShop);
		if(StringUtils.isBlank(annyShop.getId())){//如果ID是空为添加
			model.addAttribute("isAdd", true);
		}
		return "modules/anny/shop/annyShopForm";
	}


	/**
	 * 保存店铺
	 */
	@RequiresPermissions(value={"anny:shop:annyShop:add","anny:shop:annyShop:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(AnnyShop annyShop, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, annyShop)){
			return form(annyShop, model);
		}
		//新增或编辑表单保存
		annyShopService.save(annyShop);//保存
		addMessage(redirectAttributes, "保存店铺成功");
		return "redirect:"+Global.getAdminPath()+"/anny/shop/annyShop/?repage";
	}
	
	/**
	 * 删除店铺
	 */
	@ResponseBody
	@RequiresPermissions("anny:shop:annyShop:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(AnnyShop annyShop, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		annyShopService.delete(annyShop);
		j.setMsg("删除店铺成功");
		return j;
	}
	
	/**
	 * 批量删除店铺
	 */
	@ResponseBody
	@RequiresPermissions("anny:shop:annyShop:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			annyShopService.delete(annyShopService.get(id));
		}
		j.setMsg("删除店铺成功");
		return j;
	}
	
//	/**
//	 * 导出excel文件
//	 */
//	@ResponseBody
//	@RequiresPermissions("anny:shop:annyShop:export")
//    @RequestMapping(value = "export", method=RequestMethod.POST)
//    public AjaxJson exportFile(AnnyShop annyShop, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
//		AjaxJson j = new AjaxJson();
//		try {
//            String fileName = "店铺"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
//            Page<AnnyShop> page = annyShopService.findPage(new Page<AnnyShop>(request, response, -1), annyShop);
//    		new ExportExcel("店铺", AnnyShop.class).setDataList(page.getList()).write(response, fileName).dispose();
//    		j.setSuccess(true);
//    		j.setMsg("导出成功！");
//    		return j;
//		} catch (Exception e) {
//			j.setSuccess(false);
//			j.setMsg("导出店铺记录失败！失败信息："+e.getMessage());
//		}
//			return j;
//    }
//
//	/**
//	 * 导入Excel数据
//
//	 */
//	@RequiresPermissions("anny:shop:annyShop:import")
//    @RequestMapping(value = "import", method=RequestMethod.POST)
//    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
//		try {
//			int successNum = 0;
//			int failureNum = 0;
//			StringBuilder failureMsg = new StringBuilder();
//			ImportExcel ei = new ImportExcel(file, 1, 0);
//			List<AnnyShop> list = ei.getDataList(AnnyShop.class);
//			for (AnnyShop annyShop : list){
//				try{
//					annyShopService.save(annyShop);
//					successNum++;
//				}catch(ConstraintViolationException ex){
//					failureNum++;
//				}catch (Exception ex) {
//					failureNum++;
//				}
//			}
//			if (failureNum>0){
//				failureMsg.insert(0, "，失败 "+failureNum+" 条店铺记录。");
//			}
//			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条店铺记录"+failureMsg);
//		} catch (Exception e) {
//			addMessage(redirectAttributes, "导入店铺失败！失败信息："+e.getMessage());
//		}
//		return "redirect:"+Global.getAdminPath()+"/anny/shop/annyShop/?repage";
//    }
//
//	/**
//	 * 下载导入店铺数据模板
//	 */
//	@RequiresPermissions("anny:shop:annyShop:import")
//    @RequestMapping(value = "import/template")
//    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
//		try {
//            String fileName = "店铺数据导入模板.xlsx";
//    		List<AnnyShop> list = Lists.newArrayList();
//    		new ExportExcel("店铺数据", AnnyShop.class, 1).setDataList(list).write(response, fileName).dispose();
//    		return null;
//		} catch (Exception e) {
//			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
//		}
//		return "redirect:"+Global.getAdminPath()+"/anny/shop/annyShop/?repage";
//    }

}